﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace EasyDapr.Domain
{
    public abstract class EntityBase<TKey>
    {
        /// <summary>
        /// KEY
        /// </summary>
        [Key]
        public virtual TKey Id { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime Created { get; protected set; } = DateTime.UtcNow;

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? Updated { get; protected set; }

        /// <summary>
        /// 领域事件
        /// </summary>
        public HashSet<DomainEventBase> DomainEvents { get; private set; }

        /// <summary>
        /// 添加领域事件
        /// </summary>
        /// <param name="eventItem"></param>
        public void AddDomainEvent(DomainEventBase eventItem)
        {
            DomainEvents ??= new HashSet<DomainEventBase>();
            DomainEvents.Add(eventItem);
        }

        /// <summary>
        /// 移除领域事件
        /// </summary>
        /// <param name="eventItem"></param>
        public void RemoveDomainEvent(DomainEventBase eventItem)
        {
            DomainEvents?.Remove(eventItem);
        }
    }
}